﻿/*
* ---------------------------------------------------
* 作者: MTQ 
* 日期： 2021/5/14 17:13:38 
* ---------------------------------------------------
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MTQ.BLL.UploadExcel;
using System.Web;
using NPOI.SS.UserModel;
using MTQ.Model;
using MTQ.BLL;
using MTQ.IBLL;
using MTQ.Common;
using System.Data;
namespace MTQ.BLL
{
    public partial class Log_ErrorService : BaseService<Log_Error>
    {
        public IQueryable<Log_Error> LoadSearchData(Log_ErrorQuery query)
        {
            var temp = _DbSession.Log_ErrorRepository.LoadEntities(u => true);
            //可根据所需查询条件自行添加  

            if (!string.IsNullOrEmpty(query.Log_UserName))
            {
                temp = temp.Where<Log_Error>(u => u.Log_UserName.Contains(query.Log_UserName));
            }

            if (!string.IsNullOrEmpty(query.Log_Path))
            {
                temp = temp.Where<Log_Error>(u => u.Log_Path.Contains(query.Log_Path));
            }

            if (!string.IsNullOrEmpty(query.Log_HttpMethod))
            {
                temp = temp.Where<Log_Error>(u => u.Log_HttpMethod.Contains(query.Log_HttpMethod));
            }

            if (!string.IsNullOrEmpty(query.Log_QueryString))
            {
                temp = temp.Where<Log_Error>(u => u.Log_QueryString.Contains(query.Log_QueryString));
            }

            if (!string.IsNullOrEmpty(query.Log_ErrorInfo))
            {
                temp = temp.Where<Log_Error>(u => u.Log_ErrorInfo.Contains(query.Log_ErrorInfo));
            }

            if (!string.IsNullOrEmpty(query.Log_ErrorDetail))
            {
                temp = temp.Where<Log_Error>(u => u.Log_ErrorDetail.Contains(query.Log_ErrorDetail));
            }


            query.Total = temp.Count();
            return temp.OrderBy(u => u.LE_Id).Skip(query.PageSize * (query.PageIndex - 1)).Take(query.PageSize);
        }
        protected override IQueryable<Log_Error> GetBootstrapQueryable<K>(K paramterquery)
        {
            Log_ErrorQuery query = paramterquery as Log_ErrorQuery;
            IQueryable<Log_Error> temp = _DbSession.Log_ErrorRepository.LoadEntities(u => true);


            if (!string.IsNullOrEmpty(query.Log_UserName))
            {
                temp = temp.Where<Log_Error>(u => u.Log_UserName.Contains(query.Log_UserName));
            }

            if (!string.IsNullOrEmpty(query.Log_Path))
            {
                temp = temp.Where<Log_Error>(u => u.Log_Path.Contains(query.Log_Path));
            }

            if (!string.IsNullOrEmpty(query.Log_HttpMethod))
            {
                temp = temp.Where<Log_Error>(u => u.Log_HttpMethod.Contains(query.Log_HttpMethod));
            }

            if (!string.IsNullOrEmpty(query.Log_QueryString))
            {
                temp = temp.Where<Log_Error>(u => u.Log_QueryString.Contains(query.Log_QueryString));
            }

            if (!string.IsNullOrEmpty(query.Log_ErrorInfo))
            {
                temp = temp.Where<Log_Error>(u => u.Log_ErrorInfo.Contains(query.Log_ErrorInfo));
            }

            if (!string.IsNullOrEmpty(query.Log_ErrorDetail))
            {
                temp = temp.Where<Log_Error>(u => u.Log_ErrorDetail.Contains(query.Log_ErrorDetail));
            }


            return temp;
        }

        public IQueryable<Log_Error> GetIQueryable(Log_ErrorQuery query)
        {
            return this.GetBootstrapQueryable<Log_ErrorQuery>(query);
        }
        public static bool ReadExcel(string filename, Guid userid, ref string msg)
        {

            return true;
        }


        public static void SaveRequestErrorLog(HttpRequestBase httpRequest, string UserName, Exception ex)
        {

            try
            {
                Log_Error log_web = new Log_Error()
                {
                    Log_UserName = UserName,
                    Log_CreateTime = System.DateTime.Now,
                    Log_Path = httpRequest.FilePath.ToString(),
                    //Log_Remark = httpRequest.QueryString.ToString(),
                    Log_HttpMethod = httpRequest.HttpMethod,
                    Log_ErrorInfo = ex.Message,
                    Log_ErrorDetail = ex.StackTrace,
                };
                try
                {
                    //log_web.Log_QueryString = httpRequest.QueryString;
                }
                catch { }

                new Log_ErrorService().AddEntity(log_web);
            }
            catch (Exception exx)
            {
                LogManager.WriteLog("日志异常", exx);
            }
        }


    }
}

